package com.zhycheng.readerxls;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ListView;
import android.widget.SimpleAdapter;

/**
 * 
 ***************************************************** 
 * <hr>
 * <dt><span class="strong">类功能简介:</span></dt>
 * <dd>Coffee</dd>
 * <dt><span class="strong">创建时间:</span></dt>
 * <dd>2015-4-9 上午10:13:26</dd>
 * <dt><span class="strong">公司:</span></dt>
 * <dd>CorpIt</dd>
 * 
 * @author aa1000777 - Email:aa1000777@qq.com
 ***************************************************** 
 */
public class MainActivity extends Activity {
	/** Called when the activity is first created. */

	ArrayList<HashMap<String, String>> al;
	ListView lv;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		/* 读取Excel表格数据 */
		getExcel();
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 读取Excel表格数据
	 ***************************************************** 
	 */
	private void getExcel() {
		lv = (ListView) findViewById(R.id.listView1);
		al = new ArrayList<HashMap<String, String>>();
		AssetManager am = this.getAssets();
		InputStream is = null;
		try {
			is = am.open("data.xls");
			Workbook wb = Workbook.getWorkbook(is);
			Sheet sheet = wb.getSheet(0);
			int row = sheet.getRows();
			HashMap<String, String> hm;
			for (int i = 0; i < row; ++i) {
				Cell cellarea = sheet.getCell(0, i);
				Cell cellschool = sheet.getCell(1, i);
				System.out.println(cellarea.getContents() + ":" + cellschool.getContents());
				hm = new HashMap<String, String>();
				hm.put("AREA", cellarea.getContents());
				hm.put("SCHOOL", cellschool.getContents());
				al.add(hm);
			}
			SimpleAdapter sa = new SimpleAdapter(this, al, R.layout.lv_item, new String[] { "AREA", "SCHOOL" }, new int[] { R.id.tv_area, R.id.tv_school });
			lv.setAdapter(sa);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("_**************************" + al.size() + "*******************************");
		/* 将数据写成Excel表格 */
		writeDataToExcel();
	}

	/**
	 * 
	 ***************************************************** 
	 * 方法简介: 写Excel
	 * 
	 * @param list
	 ***************************************************** 
	 */
	public void writeDataToExcel() {
		System.out.println("----------------------------writeDataToExcel------------------------------");
		WritableWorkbook wwb = null;
		String fileName = Environment.getExternalStorageDirectory() + "/" + "SrtResult" + ".xls";
		int numrows = al.size();
		System.out.println("------------------看下size：" + numrows);
		System.out.println("fileName:" + fileName);
		try {
			wwb = Workbook.createWorkbook(new File(fileName));
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (wwb != null) {
			// to create a table;
			// Workbookçš„createSheet has two parametersï¼Œthe first one is the
			// name
			// of table,and the second one is the position of this table in a
			// book.
			WritableSheet ws = wwb.createSheet("sheet1", 0);
			// to add the cells
			for (int i = 0; i < numrows; i++) {
				// Note:the j means the column,the i means row
				System.out.println(i + "AREA:" + al.get(i).get("AREA"));
				System.out.println(i + "AREA:" + al.get(i).get("SCHOOL"));
				Label labelA = new Label(0, i, al.get(i).get("AREA"));
				Label labelB = new Label(1, i, al.get(i).get("SCHOOL"));
				try {
					// add the cells got just now into table;
					ws.addCell(labelA);
					ws.addCell(labelB);
				} catch (RowsExceededException e) {
					e.printStackTrace();
				} catch (WriteException e) {
					e.printStackTrace();
				}
			}
		}
		try {
			wwb.write();
			wwb.close();
		} catch (WriteException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}